home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / HTMLParser.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2005-10-18  |  10.5 KB  |  363 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. '''A parser for HTML and XHTML.'''
  5. import markupbase
  6. import re
  7. interesting_normal = re.compile('[&<]')
  8. interesting_cdata = re.compile('<(/|\\Z)')
  9. incomplete = re.compile('&[a-zA-Z#]')
  10. entityref = re.compile('&([a-zA-Z][-.a-zA-Z0-9]*)[^a-zA-Z0-9]')
  11. charref = re.compile('&#(?:[0-9]+|[xX][0-9a-fA-F]+)[^0-9a-fA-F]')
  12. starttagopen = re.compile('<[a-zA-Z]')
  13. piclose = re.compile('>')
  14. commentclose = re.compile('--\\s*>')
  15. tagfind = re.compile('[a-zA-Z][-.a-zA-Z0-9:_]*')
  16. attrfind = re.compile('\\s*([a-zA-Z_][-.:a-zA-Z_0-9]*)(\\s*=\\s*(\\\'[^\\\']*\\\'|"[^"]*"|[-a-zA-Z0-9./,:;+*%?!&$\\(\\)_#=~@]*))?')
  17. locatestarttagend = re.compile('\n  <[a-zA-Z][-.a-zA-Z0-9:_]*          # tag name\n  (?:\\s+                             # whitespace before attribute name\n    (?:[a-zA-Z_][-.:a-zA-Z0-9_]*     # attribute name\n      (?:\\s*=\\s*                     # value indicator\n        (?:\'[^\']*\'                   # LITA-enclosed value\n          |\\"[^\\"]*\\"                # LIT-enclosed value\n          |[^\'\\">\\s]+                # bare value\n         )\n       )?\n     )\n   )*\n  \\s*                                # trailing whitespace\n', re.VERBOSE)
  18. endendtag = re.compile('>')
  19. endtagfind = re.compile('</\\s*([a-zA-Z][-.a-zA-Z0-9:_]*)\\s*>')
  20.  
  21. class HTMLParseError(Exception):
  22.     '''Exception raised for all parse errors.'''
  23.     
  24.     def __init__(self, msg, position = (None, None)):
  25.         self.msg = msg
  26.         self.lineno = position[0]
  27.         self.offset = position[1]
  28.  
  29.     
  30.     def __str__(self):
  31.         result = self.msg
  32.         if self.lineno is not None:
  33.             result = result + ', at line %d' % self.lineno
  34.         
  35.         if self.offset is not None:
  36.             result = result + ', column %d' % (self.offset + 1)
  37.         
  38.         return result
  39.  
  40.  
  41.  
  42. class HTMLParser(markupbase.ParserBase):
  43.     '''Find tags and other markup and call handler functions.
  44.  
  45.     Usage:
  46.         p = HTMLParser()
  47.         p.feed(data)
  48.         ...
  49.         p.close()
  50.  
  51.     Start tags are handled by calling self.handle_starttag() or
  52.     self.handle_startendtag(); end tags by self.handle_endtag().  The
  53.     data between tags is passed from the parser to the derived class
  54.     by calling self.handle_data() with the data as argument (the data
  55.     may be split up in arbitrary chunks).  Entity references are
  56.     passed by calling self.handle_entityref() with the entity
  57.     reference as the argument.  Numeric character references are
  58.     passed to self.handle_charref() with the string containing the
  59.     reference as the argument.
  60.     '''
  61.     CDATA_CONTENT_ELEMENTS = ('script', 'style')
  62.     
  63.     def __init__(self):
  64.         '''Initialize and reset this instance.'''
  65.         self.reset()
  66.  
  67.     
  68.     def reset(self):
  69.         '''Reset this instance.  Loses all unprocessed data.'''
  70.         self.rawdata = ''
  71.         self.lasttag = '???'
  72.         self.interesting = interesting_normal
  73.         markupbase.ParserBase.reset(self)
  74.  
  75.     
  76.     def feed(self, data):
  77.         """Feed data to the parser.
  78.  
  79.         Call this as often as you want, with as little or as much text
  80.         as you want (may include '
  81. ').
  82.         """
  83.         self.rawdata = self.rawdata + data
  84.         self.goahead(0)
  85.  
  86.     
  87.     def close(self):
  88.         '''Handle any buffered data.'''
  89.         self.goahead(1)
  90.  
  91.     
  92.     def error(self, message):
  93.         raise HTMLParseError(message, self.getpos())
  94.  
  95.     __starttag_text = None
  96.     
  97.     def get_starttag_text(self):
  98.         """Return full source of start tag: '<...>'."""
  99.         return self._HTMLParser__starttag_text
  100.  
  101.     
  102.     def set_cdata_mode(self):
  103.         self.interesting = interesting_cdata
  104.  
  105.     
  106.     def clear_cdata_mode(self):
  107.         self.interesting = interesting_normal
  108.  
  109.     
  110.     def goahead(self, end):
  111.         rawdata = self.rawdata
  112.         i = 0
  113.         n = len(rawdata)
  114.         while i < n:
  115.             match = self.interesting.search(rawdata, i)
  116.             if match:
  117.                 j = match.start()
  118.             else:
  119.                 j = n
  120.             if i < j:
  121.                 self.handle_data(rawdata[i:j])
  122.             
  123.             i = self.updatepos(i, j)
  124.             if i == n:
  125.                 break
  126.             
  127.             startswith = rawdata.startswith
  128.             if startswith('<', i):
  129.                 if starttagopen.match(rawdata, i):
  130.                     k = self.parse_starttag(i)
  131.                 elif startswith('</', i):
  132.                     k = self.parse_endtag(i)
  133.                 elif startswith('<!--', i):
  134.                     k = self.parse_comment(i)
  135.                 elif startswith('<?', i):
  136.                     k = self.parse_pi(i)
  137.                 elif startswith('<!', i):
  138.                     k = self.parse_declaration(i)
  139.                 elif i + 1 < n:
  140.                     self.handle_data('<')
  141.                     k = i + 1
  142.                 else:
  143.                     break
  144.                 if k < 0:
  145.                     if end:
  146.                         self.error('EOF in middle of construct')
  147.                     
  148.                     break
  149.                 
  150.                 i = self.updatepos(i, k)
  151.                 continue
  152.             if startswith('&#', i):
  153.                 match = charref.match(rawdata, i)
  154.                 if match:
  155.                     name = match.group()[2:-1]
  156.                     self.handle_charref(name)
  157.                     k = match.end()
  158.                     if not startswith(';', k - 1):
  159.                         k = k - 1
  160.                     
  161.                     i = self.updatepos(i, k)
  162.                     continue
  163.                 else:
  164.                     break
  165.             match
  166.             if startswith('&', i):
  167.                 match = entityref.match(rawdata, i)
  168.                 if match:
  169.                     name = match.group(1)
  170.                     self.handle_entityref(name)
  171.                     k = match.end()
  172.                     if not startswith(';', k - 1):
  173.                         k = k - 1
  174.                     
  175.                     i = self.updatepos(i, k)
  176.                     continue
  177.                 
  178.                 match = incomplete.match(rawdata, i)
  179.                 if match:
  180.                     if end and match.group() == rawdata[i:]:
  181.                         self.error('EOF in middle of entity or char ref')
  182.                     
  183.                     break
  184.                 elif i + 1 < n:
  185.                     self.handle_data('&')
  186.                     i = self.updatepos(i, i + 1)
  187.                 else:
  188.                     break
  189.             match
  190.         if end and i < n:
  191.             self.handle_data(rawdata[i:n])
  192.             i = self.updatepos(i, n)
  193.         
  194.         self.rawdata = rawdata[i:]
  195.  
  196.     
  197.     def parse_pi(self, i):
  198.         rawdata = self.rawdata
  199.         match = piclose.search(rawdata, i + 2)
  200.         if not match:
  201.             return -1
  202.         
  203.         j = match.start()
  204.         self.handle_pi(rawdata[i + 2:j])
  205.         j = match.end()
  206.         return j
  207.  
  208.     
  209.     def parse_starttag(self, i):
  210.         self._HTMLParser__starttag_text = None
  211.         endpos = self.check_for_whole_start_tag(i)
  212.         if endpos < 0:
  213.             return endpos
  214.         
  215.         rawdata = self.rawdata
  216.         self._HTMLParser__starttag_text = rawdata[i:endpos]
  217.         attrs = []
  218.         match = tagfind.match(rawdata, i + 1)
  219.         k = match.end()
  220.         self.lasttag = tag = rawdata[i + 1:k].lower()
  221.         while k < endpos:
  222.             m = attrfind.match(rawdata, k)
  223.             if not m:
  224.                 break
  225.             
  226.             (attrname, rest, attrvalue) = m.group(1, 2, 3)
  227.             if not rest:
  228.                 attrvalue = None
  229.             elif "'" == "'":
  230.                 pass
  231.             elif not "'" == attrvalue[-1:]:
  232.                 if '"' == '"':
  233.                     pass
  234.                 elif '"' == attrvalue[-1:]:
  235.                     attrvalue = attrvalue[1:-1]
  236.                     attrvalue = self.unescape(attrvalue)
  237.                 
  238.             attrs.append((attrname.lower(), attrvalue))
  239.             k = m.end()
  240.             continue
  241.             attrvalue[:1]
  242.         end = rawdata[k:endpos].strip()
  243.         if end not in ('>', '/>'):
  244.             (lineno, offset) = self.getpos()
  245.             if '\n' in self._HTMLParser__starttag_text:
  246.                 lineno = lineno + self._HTMLParser__starttag_text.count('\n')
  247.                 offset = len(self._HTMLParser__starttag_text) - self._HTMLParser__starttag_text.rfind('\n')
  248.             else:
  249.                 offset = offset + len(self._HTMLParser__starttag_text)
  250.             self.error('junk characters in start tag: %r' % (rawdata[k:endpos][:20],))
  251.         
  252.         if end.endswith('/>'):
  253.             self.handle_startendtag(tag, attrs)
  254.         else:
  255.             self.handle_starttag(tag, attrs)
  256.             if tag in self.CDATA_CONTENT_ELEMENTS:
  257.                 self.set_cdata_mode()
  258.             
  259.         return endpos
  260.  
  261.     
  262.     def check_for_whole_start_tag(self, i):
  263.         rawdata = self.rawdata
  264.         m = locatestarttagend.match(rawdata, i)
  265.         if m:
  266.             j = m.end()
  267.             next = rawdata[j:j + 1]
  268.             if next == '>':
  269.                 return j + 1
  270.             
  271.             if next == '/':
  272.                 if rawdata.startswith('/>', j):
  273.                     return j + 2
  274.                 
  275.                 if rawdata.startswith('/', j):
  276.                     return -1
  277.                 
  278.                 self.updatepos(i, j + 1)
  279.                 self.error('malformed empty start tag')
  280.             
  281.             if next == '':
  282.                 return -1
  283.             
  284.             if next in 'abcdefghijklmnopqrstuvwxyz=/ABCDEFGHIJKLMNOPQRSTUVWXYZ':
  285.                 return -1
  286.             
  287.             self.updatepos(i, j)
  288.             self.error('malformed start tag')
  289.         
  290.         raise AssertionError('we should not get here!')
  291.  
  292.     
  293.     def parse_endtag(self, i):
  294.         rawdata = self.rawdata
  295.         match = endendtag.search(rawdata, i + 1)
  296.         if not match:
  297.             return -1
  298.         
  299.         j = match.end()
  300.         match = endtagfind.match(rawdata, i)
  301.         if not match:
  302.             self.error('bad end tag: %r' % (rawdata[i:j],))
  303.         
  304.         tag = match.group(1)
  305.         self.handle_endtag(tag.lower())
  306.         self.clear_cdata_mode()
  307.         return j
  308.  
  309.     
  310.     def handle_startendtag(self, tag, attrs):
  311.         self.handle_starttag(tag, attrs)
  312.         self.handle_endtag(tag)
  313.  
  314.     
  315.     def handle_starttag(self, tag, attrs):
  316.         pass
  317.  
  318.     
  319.     def handle_endtag(self, tag):
  320.         pass
  321.  
  322.     
  323.     def handle_charref(self, name):
  324.         pass
  325.  
  326.     
  327.     def handle_entityref(self, name):
  328.         pass
  329.  
  330.     
  331.     def handle_data(self, data):
  332.         pass
  333.  
  334.     
  335.     def handle_comment(self, data):
  336.         pass
  337.  
  338.     
  339.     def handle_decl(self, decl):
  340.         pass
  341.  
  342.     
  343.     def handle_pi(self, data):
  344.         pass
  345.  
  346.     
  347.     def unknown_decl(self, data):
  348.         self.error('unknown declaration: %r' % (data,))
  349.  
  350.     
  351.     def unescape(self, s):
  352.         if '&' not in s:
  353.             return s
  354.         
  355.         s = s.replace('<', '<')
  356.         s = s.replace('>', '>')
  357.         s = s.replace(''', "'")
  358.         s = s.replace('"', '"')
  359.         s = s.replace('&', '&')
  360.         return s
  361.  
  362.  
  363.